package com.arsn.myf.flow.controller;

import io.swagger.annotations.Api;
import org.flowable.engine.IdentityService;
import org.flowable.idm.api.Group;
import org.flowable.idm.api.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * 用户组管理
 */
@RestController
@RequestMapping("/userGroup")
@Api(value = "用户组测试管理",tags = "用户组测试管理")
public class UserGroupController {

    @Autowired
    private IdentityService identityService;

    /**
     * 新增用户
     */
    @GetMapping("/addUser")
    public void addUser(){
        User user = identityService.newUser("lisi");
        user.setFirstName("li");
        user.setLastName("si");
        user.setPassword("666666");
        //新增
        identityService.saveUser(user);
    }

    /**
     * 新增用户组
     */
    @GetMapping("/addUserGroup")
    public void addUserGroup(){
        Group group = identityService.newGroup("manager");
        group.setName("manager");
        group.setType("m");
        //新增组
        identityService.saveGroup(group);
    }


    /**
     * 新增用户到用户组
     */
    @GetMapping("/addUserToGroup")
    public void addUserToGroup(){
        Group group = identityService.createGroupQuery().groupId("manager").singleResult();
        List<User> list = identityService.createUserQuery().list();
        list.forEach(user -> {
            //新增组
            identityService.createMembership(user.getId(),group.getId());
        });

    }
}
